// pages/my/index.js
const app = getApp();
const action = require('../../utils/action.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stat:{bidding:0,win:0,complete:0},
    userInfo:{},
    customer:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ userInfo: app.globalData.userInfo});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log("load user data");
    console.log(app.globalData.userInfo);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.customer)
    {
      this.setData({
        customer: app.globalData.customer
      });
    }

    this.loadStat();
    this.loadCustomer();
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  loadCustomer: function() {
    var self = this;
    app.httpGet('/index.php?r=v1/account/index', {
      success:function(res){
        console.log(res);
        if(res.code == 200)
        {
          self.setData({customer:res.data});
          app.globalData.customer = self.customer;
        }
      }
    });
  },
  bindMobile: function(e){
    var self = this;
    console.log(e.detail.errMsg);
    console.log(e.detail.iv);
    console.log(e.detail.encryptedData);

    app.httpPut('/index.php?r=v1/phone/index', {
      success:function(res){
        if(res.code == 200)
        {
          app.success("绑定成功");
          self.setData({customer:res.data});
        }
        else
        {
          app.toast("绑定失败,请稍后重试");
        }
      }
    },{
      iv:e.detail.iv,
      encryptedData: e.detail.encryptedData
    });
  },
  payDeposit: function(e){
    wx.navigateTo({
      url: '/pages/my/paydeposit',
    })
  },
  refundDeposit: function(){
    var self = this;
    app.httpDelete('/index.php?r=v1/deposit/index',{
      success: function(res){
        if(res.code==200)
        {
          app.toast("你的申请已提交请等待退还");
          self.loadCustomer();
        }
        else
        {
          app.toast(res.msg);
        }
      }
    });
  },
  loadStat: function(){
    var self = this;
    app.httpGet('/index.php?r=v1/stat/stat',{
      success:function(res){
        if(res.code == 200)
        {
          self.setData({stat:res.data});
        }
      }
    });
  },
  onStatClick: function(e)
  {
    var status = e.currentTarget.dataset.status;
    console.log(status);
    wx.navigateTo({
      url: '/pages/my/stat?status='+status,
    })
  }
})